<template>
  <el-form ref="form" :model="readerInfo" label-width="80px">
    <el-form-item label="读者名称">
      <el-input v-model="readerInfo.readerName"></el-input>
    </el-form-item>
    <el-form-item label="读者电话">
      <el-input v-model="readerInfo.readerTel"></el-input>
    </el-form-item>
    <el-form-item label="读者密码">
      <el-input v-model="readerInfo.readerPassword"></el-input>
    </el-form-item>
    <el-form-item label="读者邮箱">
      <el-input v-model="readerInfo.readerEmail"></el-input>
    </el-form-item>
    <el-form-item label="读者余额">
    <el-input v-model="readerInfo.readerBalance"></el-input>
  </el-form-item>
    <el-form-item label="充值总额">
      <el-input v-model="readerInfo.readerRechargeSum"></el-input>
    </el-form-item>
    <el-form-item label="会员等级">
      <el-input v-model="readerInfo.readerRole"></el-input>
    </el-form-item>
    <el-form-item label="月票数">
      <el-input v-model="readerInfo.readerTicket"></el-input>
    </el-form-item>
    <el-form-item label="读者头像">
      <el-upload
        class="avatar-uploader"
        action="http://localhost:8888/reader/uploadPic"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="readerInfo.readerPic" :src="readerInfo.readerPic" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即修改</el-button>
      <el-button>取消</el-button>
    </el-form-item>

  </el-form>
</template>

<script>
  import axios from 'axios';
    export default {
        name: "readerInfo",
        data(){
          return{
            readerInfo:{
              readerName:'',
              readerTel:'',
              readerPassword:'',
              readerEmail:'',
              readerBalance:'',
              readerRechargeSum:'',
              readerRole:'',
              readerTicket:'',
              readerPic:''

            }
          }
        },
        methods:{
          findReaderById(id){
            axios.post('http://localhost:8888/reader/findById',{id:id}).then(res=>{
              if(res.data.code==0){
                this.readerInfo=res.data.data;
              }
            })
          },
          onSubmit() {
            axios.post('http://localhost:8888/reader/update',this.readerInfo).then(res=>{
              if(res.data.code==0){
                this.$router.push("2");
              }
            })

          },
          handleAvatarSuccess(res) {
            console.log(res);
            this.readerInfo.readerPic=res;
          },
          beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
              this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
              this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
          }
        },
        mounted() {
            var id = this.$route.params.id;
            if(id!=null){
              this.findReaderById(id);
            }

        }
    }
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
